package cn.modoumama.excelUilt.common;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Common {
	Logger logger = LoggerFactory.getLogger(Common.class);
    //2010之前的EXCEL后缀名
    public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";
    //2010之后的EXCEL后缀名
    public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";

    //空字符串
    public static final String EMPTY = "";
    //点
    public static final String POINT = ".";

    /**
     * 通过获取文件后缀名
     *
     * @param path 文件路径
     * @return 获取后缀命名
     */
    public static String getPostfix(String path) {
        //传入空串，null，空白符
        if (path != null) {
            path = path.trim();
            //路径中含有".",则匹配后缀名
            if (path.contains(Common.POINT)) {
                return path.substring(path.lastIndexOf(Common.POINT) + 1, path.length());
            }
        }

        return Common.EMPTY;
    }

    /**
     * 首字母转大写
     *
     * @param String 需要转换的字符串
     * @return
     */
    public static String toUpperCaseFirstOne(String s) {
        if (Character.isUpperCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder())
                    .append(Character.toUpperCase(s.charAt(0)))
                    .append(s.substring(1)).toString();
    }

    @SuppressWarnings("unchecked")
    public static <T> T parsValue(Class<T> clazz, String value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
        if (null != value) {
            value = value.trim();
            if (clazz == String.class) {
                return (T) value;
            } else if (!EMPTY.equals(value)) {
                //基本类型
                if (clazz.isPrimitive()) {
                    return (T) parsTypeValue(clazz, value);
                }
                //包装类
                return (T) clazz.getMethod("valueOf", String.class).invoke(null, value);
            }
        }
        return null;
    }

    /**
     * 解析时间
     *
     * @param clazz 需要转换成的类型
     * @param value java.util.Date
     * @return 返回传入的值
     */
    public static <T> Object parsDate(Class<T> clazz, Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        if (clazz == java.util.Date.class) {
            return date;
        } else if (clazz == java.sql.Date.class) {
            return new java.sql.Date(date.getTime());
        } else if (clazz == java.sql.Time.class) {
            return new java.sql.Time(date.getTime());
        } else if (clazz == java.sql.Timestamp.class) {
            return new java.sql.Timestamp(date.getTime());
        }
        return sdf.format(date);
    }

    /**
     * 把字符串解析成基本类型
     *
     * @param clazz 需要转换成的类型
     * @param value 字符串
     * @return 返回传入的值
     */
    public static <T> Object parsTypeValue(Class<T> clazz, String value) {
        if (clazz == Byte.TYPE) {
            return Byte.parseByte(value);
        } else if (clazz == Short.TYPE) {
            return Short.parseShort(value);
        } else if (clazz == Integer.TYPE) {
            return Integer.parseInt(value);
        } else if (clazz == Long.TYPE) {
            return Long.parseLong(value);
        } else if (clazz == Boolean.TYPE) {
            return Boolean.parseBoolean(value);
        } else if (clazz == Character.TYPE) {
            return value.charAt(0);
        } else if (clazz == Float.TYPE) {
            return Float.parseFloat(value);
        } else {
            return Double.parseDouble(value);
        }
    }
    
    public static Class<?> getPropertyType(Class<?> clazz, String fieldName) {
         String[] fieldNames = fieldName.split("\\.");
         PropertyDescriptor prop = null;
         for (int i = 0; i < fieldNames.length; i++) {
        	 try {
        		 if(Map.class.isAssignableFrom(clazz)){
        			 clazz = Object.class;
        			 break;
        		 }else{
        			 prop = new PropertyDescriptor(fieldNames[i], clazz);
    	        	 clazz = prop.getPropertyType();
        		 }
	        	 
        	 } catch (IntrospectionException |  IllegalArgumentException e) {
				throw new RuntimeException(e);
			}
		}
		return clazz;
    }
    
}
